/** for Color */
import java.awt.Color;

/** for Dimension */
import java.awt.Dimension;

/** for Font */
import java.awt.Font;

/** for Graphics */
import java.awt.Graphics;

/** for Image */
import java.awt.Image;

/** for ActionEvent */
import java.awt.event.ActionEvent;

/** for ActionListener */
import java.awt.event.ActionListener;

/** for ArrayList */
import java.util.ArrayList;
import java.util.List;

/** ImageIcon */
import javax.swing.ImageIcon;

/** JLabel */
import javax.swing.JLabel;

/** JOptionPane */
import javax.swing.JOptionPane;

/** JPanel */
import javax.swing.JPanel;

/** JButton */
import javax.swing.JButton;

/**
 * This is a class Panel for Map
 * @author Code Traders
 * @version 1.0
 */
public class MapPanel extends JPanel implements ActionListener, MainPanel {
	/**for Singleton**/
	private static final long serialVersionUID = 3466087060692236956L;
	
	/**WIDTH, HEIGHT**/
	private static final  int WIDTH = 640, HEIGHT = 252;
	
	/**to get planet from ArrayList**/
	private final List<Planet> planets;
	
	/**to get ship**/
	private final Ship ship;
	
	/**for JLabel**/
	private final JLabel lblX, lblX2;
	
	/**for JButtion**/
	private final JButton moveBtn;
	
	/**for JButtion with ArrayList**/
	private final List<JButton> buttons = new ArrayList<JButton>();
	
	/**to get main Panel**/
	private SpaceTraderMainGUI mainPanel;
	
	/**
	 * This is a constructor for MapPanel
	 */
	@SuppressWarnings("unchecked")
	public MapPanel() {
		super.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		super.setLayout(null);
		
		moveBtn = new JButton("Move");
		moveBtn.setBounds(Numbers.FIVE_HUNDRED.getNumber() 
				+ Numbers.FORTY.getNumber() + Numbers.SIX.getNumber(), 
				Numbers.TWO_HUNDRED.getNumber() + Numbers.TWENTY.getNumber() 
				+ Numbers.THREE.getNumber(), Numbers.SIXTY.getNumber() 
				+ Numbers.SEVEN.getNumber(), Numbers.TWNETY_SIX.getNumber() 
				+ Numbers.THREE.getNumber());
		moveBtn.addActionListener(this);
		moveBtn.setActionCommand("move");
		super.add(moveBtn);
		
		lblX = new JLabel("X");
		lblX.setFont(new Font("Lucida Grande", Font.BOLD, Numbers.TWENTY.getNumber()));
		lblX.setForeground(Color.CYAN);
		super.add(lblX);
		lblX.setVisible(false);
		
		lblX2 = new JLabel("X");
		lblX2.setFont(new Font("Lucida Grande", Font.BOLD, Numbers.TWENTY.getNumber()));
		lblX2.setForeground(Color.RED);
		super.add(lblX2);
		lblX2.setVisible(false);
		
		/** Singleton **/
		this.planets = (ArrayList<Planet>) Singleton.INSTANCE.get("planets");
		this.ship = (Ship) Singleton.INSTANCE.get("ship");
		/** Singleton **/
	}
	
	/**
	 * show appropriate location where the user is and show proper images.
	 * @param g
	 */
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (Planet p : planets) {
			int x = p.getPoint().getX();
			int y = p.getPoint().getY();
			if (buttons.size() != planets.size()) {
				JButton btn = new JButton();
				btn.setToolTipText(p.getName());
				btn.setPreferredSize(new Dimension(p.getWidth(), p.getHeight()));
				btn.setBounds(x, y, p.getWidth(), p.getHeight());
				Image img = new ImageIcon(SpaceTraderMainGUI.class.getResource
						("/image/p" + p.getResource().ordinal() + ".png")).getImage();
				img = img.getScaledInstance(p.getWidth(),
						p.getHeight(), java.awt.Image.SCALE_SMOOTH);
				btn.setIcon(new ImageIcon(img));
				btn.setActionCommand(p.getName());
				btn.setOpaque(false);
				btn.setContentAreaFilled(false);
				btn.setBorderPainted(false);
				btn.addActionListener(this);
				buttons.add(btn);
				add(btn);
			}
			if (p.equals(ship.getTempPlanet())) {
				lblX2.setBounds(x + p.getWidth() / Numbers.FIVE.getNumber(), 
						y + p.getHeight() / Numbers.FIVE.getNumber(), 
						Numbers.FOUR.getNumber() + Numbers.TEN.getNumber(), 
						Numbers.SIX.getNumber() + Numbers.TEN.getNumber());
				lblX2.setVisible(true);
			}
		}
		lblX.setBounds(ship.getCurrentPlanet().getPoint().getX() +
				ship.getCurrentPlanet().getWidth() / Numbers.FIVE.getNumber(),
				ship.getCurrentPlanet().getPoint().getY() +
				ship.getCurrentPlanet().getHeight() / Numbers.FIVE.getNumber(), 
				Numbers.FOUR.getNumber() + Numbers.TEN.getNumber(), 
				Numbers.SIX.getNumber() + Numbers.TEN.getNumber());
		lblX.setVisible(true);
	}
	
	/**
	 * to show what is updated
	 */
	@Override
	public void update() {
		repaint();
		mainPanel.updateBottomPanel();
	}
	
	/**
	 * to set main Panel
	 * @param m
	 */
	public void setMainPanel(SpaceTraderMainGUI m) {
		this.mainPanel = m;
	}

	/**
	 * to actionPerformed
	 * @param e
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (!e.getActionCommand().equals("move")) {
			Planet p_tmp = null;
			for (Planet p : planets) {
				if (p.getName().equals(e.getActionCommand())) {
					p_tmp = p;
					break;
				}
			}
			if (ship.canTravelTo(ship.getCurrentPlanet(), p_tmp)) {
				//ship.moveToPlanet(p_tmp);
				ship.setTempPlanet(p_tmp);
			} else {
				JOptionPane.showMessageDialog(this,
						"Not enough fuel to go that far...",
						"Failed to move", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			final List<String> planets_str = new ArrayList<String>();
			for (Planet p : planets) {
				planets_str.add(p.getName());
			}
			final int num = planets_str.indexOf(JOptionPane.showInputDialog
					(this, "Where to move?", "Space Trader", JOptionPane.QUESTION_MESSAGE,
							null, planets_str.toArray(), planets_str.toArray()[0]));
			if (num != -1) {
				if (ship.canTravelTo(ship.getCurrentPlanet(), planets.get(num))) {
					ship.moveToPlanet(planets.get(num));
				} else {
					JOptionPane.showMessageDialog(this,
					"Not enough fuel to go that far...",
					"Failed to move", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		this.update();
	}
}